package com.zjw.zy.componse.unityui.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * @author ：zhong.jw
 * @date ：Created in 2023/3/14 11:00
 * 移动手柄View
 */
public class MoveView extends View {

    private float mHorizontal = 0f;
    private float mVertical = 0f;
    private final Paint outPaint = new Paint();
    private final Paint innerPaint = new Paint();
    private Listener mListener;
    private float mCurrentTranslationX = 0f;
    private float mCurrentTranslationY = 0f;

    public MoveView(Context context) {
        super(context);
    }

    public MoveView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MoveView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public MoveView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        outPaint.setColor(Color.GRAY);
        outPaint.setAlpha(150);

        innerPaint.setColor(Color.GRAY);
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent event) {

        int action = event.getActionMasked();
        float x = event.getX();
        float y = event.getY();
        float cx = getMeasuredWidth() * 0.5f;
        float cy = getMeasuredHeight() * 0.5f;
        float outRadius = Math.min(cx, cy);
        float moveX = x - cx;
        float moveY = y - cy;

        if (action == MotionEvent.ACTION_DOWN) {
            //move this
            setTranslationX(moveX + mCurrentTranslationX);
            setTranslationY(moveY + mCurrentTranslationY);
            if (mListener != null) {
                mListener.onMoveStart();
            }
        }

        if (action == MotionEvent.ACTION_MOVE) {
            //start moving
            double sqrt = Math.sqrt(Math.pow(moveX, 2.0) + Math.pow(moveY, 2.0));
            double moveDistance = Math.min(sqrt, outRadius);
            // 0.0~1.0
            double moveFloat = moveDistance / outRadius;
            //分配到x，y方向
            mHorizontal = (float) (moveFloat * moveX / sqrt);
            mVertical = (float) (moveFloat * moveY / sqrt);
        }

        if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
            //stop moving
            mHorizontal = 0f;
            mVertical = 0f;
        }

        if (mListener != null) {
            mListener.onMove(mHorizontal, mVertical);
        }

        if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
            mCurrentTranslationX = getTranslationX();
            mCurrentTranslationY = getTranslationY();

            if (mListener != null) {
                mListener.onMoveEnd();
            }
        }

        invalidate();
        return true;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int height = getMeasuredHeight();
        int width = getMeasuredWidth();

        float outRadius = Math.min(width, height) * 0.5f;
        float innerRadius = outRadius * 0.4f;
        float cx = width * 0.5f;
        float cy = height * 0.5f;

        //绘制外圆
        canvas.drawCircle(cx, cy, outRadius, outPaint);

        //绘制内圆
        float maxDeviation = outRadius - innerRadius;
        canvas.drawCircle(cx + mHorizontal * maxDeviation, cy + mVertical * maxDeviation, innerRadius, innerPaint);
    }


    public void bindListener(Listener listener) {
        mListener = listener;
    }


    public interface Listener {

        void onMoveStart();

        /**
         * 移动中回调
         *
         * @param horizontal:水平方向偏移 -1.0~1.0
         * @param vertical:垂直方向偏移   -1.0~1.0
         */
        void onMove(float horizontal, float vertical);

        void onMoveEnd();
    }
}
